import Vue from 'vue'
const state = {
    cartList: [{
            "isSelected": false,
            "count": 2,
            "promId": 0,
            "showPoints": false,
            "itemTagList": [{
                "itemId": 1536001,
                "tagId": 128111157,
                "freshmanExclusive": false,
                "name": "暖冬特惠",
                "subType": 204,
                "forbidJump": false,
                "type": 2
            }],
            "rank": 1,
            "id": 1536001,
            "sellVolume": 3634,
            "primaryPicUrl": "https://yanxuan-item.nosdn.127.net/32b8b2d07b1c4327593a4a70993eeac2.png",
            "soldOut": false,
            "sortFlag": 0,
            "commentCount": 0,
            "onSaleTime": 1538101896296,
            "picMode": 1,
            "commentWithPicCount": 0,
            "underShelf": false,
            "status": 2,
            "couponConflict": true,
            "forbiddenBuy": false,
            "promotionDesc": "暖冬特惠",
            "limitedFlag": 204,
            "pieceNum": 0,
            "itemSizeTableDetailFlag": false,
            "forbidExclusiveCal": false,
            "rewardShareFlag": false,
            "updateTime": 1575894115275,
            "showCommentEntrance": true,
            "pieceUnitDesc": "件",
            "specialPromTag": "",
            "counterPrice": 299,
            "categoryL2Id": 0,
            "retailPrice": 209,
            "primarySkuPreSellPrice": 0,
            "preLimitFlag": 0,
            "itemPromValid": true,
            "promTag": "暖冬特惠",
            "source": 0,
            "points": 0,
            "primarySkuPreSellStatus": 0,
            "extraServiceFlag": 0,
            "flashPageLink": "",
            "autoOnsaleTimeLeft": 0,
            "innerData": {},
            "saleCenterSkuId": 0,
            "pointsStatus": 0,
            "extraPrice": "",
            "colorNum": 0,
            "showTime": 0,
            "autoOnsaleTime": 0,
            "preemptionStatus": 1,
            "isPreemption": 0,
            "zcSearchFlag": false,
            "name": "女式色拉姆内衣套装2.0",
            "appExclusiveFlag": false,
            "itemType": 1,
            "listPicUrl": "https://yanxuan-item.nosdn.127.net/02b61fb5700aed6761b7524d98ed0837.png",
            "pointsPrice": 0,
            "simpleDesc": "色拉姆发热面料，加厚升级",
            "seoTitle": "",
            "newItemFlag": false,
            "buttonType": 0,
            "primarySkuId": 1634105,
            "displaySkuId": 1634104,
            "productPlace": "",
            "itemSizeTableFlag": false
        },
        {
            "isSelected": true,
            "count": 1,
            "promId": 0,
            "showPoints": false,
            "itemTagList": [{
                "itemId": 1562007,
                "tagId": 128111157,
                "freshmanExclusive": false,
                "name": "暖冬特惠",
                "subType": 204,
                "forbidJump": false,
                "type": 2
            }],
            "rank": 1,
            "id": 1562007,
            "sellVolume": 2477,
            "primaryPicUrl": "https://yanxuan-item.nosdn.127.net/eade4561af1081945c35ba934c7348b1.png",
            "soldOut": false,
            "sortFlag": 0,
            "commentCount": 0,
            "onSaleTime": 1536637196215,
            "picMode": 1,
            "commentWithPicCount": 0,
            "underShelf": false,
            "status": 2,
            "couponConflict": true,
            "forbiddenBuy": false,
            "promotionDesc": "暖冬特惠",
            "limitedFlag": 204,
            "pieceNum": 0,
            "itemSizeTableDetailFlag": false,
            "forbidExclusiveCal": false,
            "rewardShareFlag": false,
            "updateTime": 1576742994144,
            "showCommentEntrance": true,
            "pieceUnitDesc": "件",
            "specialPromTag": "",
            "counterPrice": 299,
            "categoryL2Id": 0,
            "retailPrice": 199,
            "primarySkuPreSellPrice": 0,
            "preLimitFlag": 0,
            "itemPromValid": true,
            "promTag": "暖冬特惠",
            "source": 0,
            "points": 0,
            "primarySkuPreSellStatus": 0,
            "extraServiceFlag": 0,
            "flashPageLink": "",
            "autoOnsaleTimeLeft": 0,
            "innerData": {},
            "saleCenterSkuId": 0,
            "pointsStatus": 0,
            "extraPrice": "",
            "colorNum": 4,
            "showTime": 0,
            "autoOnsaleTime": 0,
            "preemptionStatus": 1,
            "isPreemption": 0,
            "zcSearchFlag": false,
            "name": "不扎脖子的柔软，男式可机洗高领羊毛衫",
            "appExclusiveFlag": false,
            "itemType": 1,
            "listPicUrl": "https://yanxuan-item.nosdn.127.net/bb4c991ad08c0b061c083cd7b2f2dd08.png",
            "pointsPrice": 0,
            "simpleDesc": "轻薄打底告别臃肿",
            "seoTitle": "",
            "newItemFlag": false,
            "buttonType": 0,
            "primarySkuId": 1623203,
            "displaySkuId": 1623197,
            "productPlace": "",
            "itemSizeTableFlag": false
        }
    ]
}

const mutations = {
    //修改 添加
    //1.购物车已经有该商品，让原来商品数量count+1
    //2.购物车没有该商品，直接添加到购物车
    addGoodsItemMutation(state, goodsItem) {
        const item = state.cartList.find(item => item.id === goodsItem.id)
        if (item) {
            //如果存在这个商品就让他加一
            item.count += 1;
        } else {
            //如果这个商品不存在的话
            // goodsItem.count=1;
            // goodsItem.isSelected=true;
            Vue.set(goodsItem, 'count', 1);
            Vue.set(goodsItem, 'isSelected', true);
            state.cartList.push(goodsItem);
        }
    },
    changeCountMustation(state, {isAdd,index}) {
        //添加或者删除商品
        if (isAdd) {
            state.cartList[index].count += 1;
        } else {
            //减
            if (state.cartList[index].count > 1) {
                state.cartList[index].count -= 1;
            } else {
                wx.showModal({
                    title: '提示',
                    content: '您确定要删除该商品吗？',
                    success(res) {
                        if (res.confirm) {
                            console.log('用户点击确定')
                            //删除
                            state.cartList.splice(index, 1);
                        } else if (res.cancel) {
                            console.log('用户点击取消')
                        }
                    }
                })
            }
        }
    },
        //修改商品的选中状态
        changeSelectedMustation(state,{isSelected,index}){
            state.cartList[index].isSelected=isSelected;
            // console.log(isSelected,index)
        },
        //修改所有的商品的选中状态
        changeAllSelectMutation(state,isAllSelected){
            //让所有的商品的选中状态，跟全选按钮保持一致
            state.cartList.forEach(item=>item.isSelected=isAllSelected)
        }
}

const actions = {

}

const getters = {
    //是否全选中，全部为真就是真，只要有一个是false，就为false =>every
    isAllSelected(state){
        return state.cartList.every(item=>item.isSelected==true)
    },
    //总数量  外面的return加完后再返回 ，里面的return是累加
    totalCount(state){
        //pre是可以随便取名的，意思是初始值  
        return state.cartList.reduce((pre,item)=>{
            return pre+=item.isSelected? item.count:0
        },0)
    },
    //总价格：商品单价*商品数量
    totalPrice(state){
        return state.cartList.reduce((pre,item)=>{
            return pre+=item.isSelected? item.retailPrice*item.count:0
        },0)
    }
}

export default {
    namespaced: true, //分模块处理
    state,
    mutations,
    actions,
    getters
}
